﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;

namespace Interface.Utility.Helper
{
    /// <summary>
    /// 数据加密
    /// </summary>
    public static class EncryptHelper
    {
        /// <summary>
        /// 加密数据
        /// </summary>
        /// <param name="text">文本</param>
        /// <param name="key">密匙</param>
        /// <returns>密文</returns>
        public static string Encrypt(string text, string key = "interface")
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(text);
            des.Key = ASCIIEncoding.ASCII.GetBytes(
                FormsAuthentication.HashPasswordForStoringInConfigFile(
                key, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(
                FormsAuthentication.HashPasswordForStoringInConfigFile(
                key, "md5").Substring(0, 8));
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }

        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="text">密文</param>
        /// <param name="key">密匙</param>
        /// <returns>文本</returns>
        public static string Decrypt(string text, string key = "interface")
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            int len = text.Length / 2;
            byte[] inputByteArray = new byte[len];
            int x, i;
            // 把加密以后的数据放到字节数组中
            for (x = 0; x < len; x++)
            {
                i = Convert.ToInt32(text.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(
                FormsAuthentication.HashPasswordForStoringInConfigFile(
                key, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(
                FormsAuthentication.HashPasswordForStoringInConfigFile(
                key, "md5").Substring(0, 8));
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }

        /// <summary>
        /// 不可逆加密
        /// </summary>
        /// <param name="text">加密文本</param>
        /// <returns>密文</returns>
        public static string EncryptMD5(string text)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(text, "MD5");
        }

        /// <summary>
        /// 不可逆加密
        /// </summary>
        /// <param name="text">加密文本</param>
        /// <returns>密文</returns>
        public static string EncryptSHA1(string text)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(text, "SHA1");
        }
    }
}